<?php
/*******************************************************************************
*
* @Author Kevin Kunkel <kevin.kunkel@marist.edu>
*
* CMBGenericWebService contains an instance of SoapClient that connects to the 
* eclient and is used to search and retrieve content. There are three functions:
* search( $query ), getItem( $URI ), and getLastRequest(). 
*
* search performs a search with the given query. This must be a properly formed
* Content Manager search query. See IBM documentation for examples. It returns
* an XML document containing the pertinent metadata, including the URI.
*
* getItem retrieves a specific item given the URI that search returns. As
* currently implemented, it will render the content. 
*
*******************************************************************************/

class ESSearch{
   public $client;
	protected $app = "Default";

   public function ESSearch($appID){//$database, $user, $password){
		$this->app = $appID;
      $this->client = new SoapClient( "http://cmeclient.is.marist.edu/ESSearchServer/wsdl/com/ibm/es/ws6/server/search/ofsearch.wsdl",
			array('trace' => true, 'exceptions' => true,
                'features' => SOAP_USE_XSI_ARRAY_TYPE + SOAP_SINGLE_ELEMENT_ARRAYS ) );

   }
   public function search($query, $page_size = 50, $page_num = 0){
		$q = "";
		$qw = explode(" ", $query);
		foreach($qw as $word){
			$q .= "*".$word."* ";
		}
		$q = substr($q,0,-1);
		try{
			$result = $this->client->getAvailableFields(array("ID"=> $this->app,"password"=>"","token"=>""));
			$retFields = array();
			foreach($result->fields as $field){
				if($field->returnable){
					$retFields[] = array("name"=>$field->iD);
				}
			}
			$parameters = array('collectionIDs'=>array(),
                     'applicationInfo'=>array("ID"=>$this->app,"password"=>"","token"=>""),
                     'linguisticMode'=>TRUE,
                     'queryText'=>$q,
							'predefinedResultsEnabled'=>FALSE,
                     'firstRequestedResult'=>$page_size*$page_num,
                     'resultCategoriesDetailLevel'=>1,
                     'sortOrder'=>1,
                     'sortPoolSize'=>500,
                     'synonymExpansionMode'=>"SYNONYM_EXPANSION_AUTOMATIC",
                     'spellCorrectionEnabled'=>TRUE,
                     'siteCollapsingEnabled'=>FALSE,
                     'returnedFields'=> $retFields,
                     'properties'=>array('FuzzyNGramSearch' => TRUE, 'FuzzyNGramAmbiguity' => 0.3, ),
                     'sortKeyByLocale'=>array('key'=>'score','locale'=>array('language'=>'en','country'=>'US')),
                     'returnAttribute'=>array('attributes'=>array('attribute'=>-3, 'returned' => true)),
                     'clientLocale'=>array('language'=>'en','country'=>'US'),
                     'numRequestedResults'=>$page_size);
		$result = $this->client->search($parameters);
	} catch( SoapFault $fault) {
			echo "REQUEST:\n" . $this->client->__getLastRequest() . "\n";
			print("SOAP Fault: (faultcode: {$fault->faultcode}\n" .
                                "faultstring: {$fault->faultstring})");
      }
		return $result;
   }
   public function getItemData($URI){
		return "/ESSearchApplication/".$URI;
   }
   public function getFields(){
		try{
			$result = $this->client->getAvailableFields(array("ID"=> $this->app, "password"=>"", "token"=>"" ));
         $retFields = array();
         foreach($result->fields as $field){
            if($field->returnable){
               $retFields[] = array("name"=>$field->iD);
            }
         }
		} catch (SoapFault $fault) {
         echo "REQUEST:\n" . $this->client->__getLastRequest() . "\n";
         print("SOAP Fault: (faultcode: {$fault->faultcode}\n" .
                                "faultstring: {$fault->faultstring})");
      }
		return $retFields;
   }
   public function getLastRequest(){
      return $this->client->__getLastRequest();
   }
}
// vim: ts=3:sw=3
?>
